﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using TracNghiemV1.Database;
using TracNghiemV1.DTO;
using TracNghiemV1.BUS;
using TracNghiemV1.DAO;
using TracNghiemV1.Utilities;
using MyManageViewControl;


namespace TracNghiemV1
{
    public partial class Form1 : RibbonForm
    {
        ManageView m_managementView;

        SYSUSER loginUser;

        public Form1()
        {
            InitializeComponent();
            InitSkinGallery();
            InitNavbarGroupControls();

            m_managementView = null;
            loginUser = null;
            
        }

        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }

        private void barButtonItemExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barButtonItemLogIn_ItemClick(object sender, ItemClickEventArgs e)
        {
            LoginDialog loginDlg = new LoginDialog();
            loginDlg.ShowDialog();

            loginUser = loginDlg.LoginUser;

            if (null == loginUser)
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_LOGIN, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (UserRoleDto.ADMIN_ROLE == loginUser.USER_ROLE)
            {
                this.navBarGroupAdmin.Visible = true;
            }
            else if (UserRoleDto.LECTURER_ROLE == loginUser.USER_ROLE)
            {
                this.navBarGroupLecturer.Visible = true;
            }
            else if (UserRoleDto.STUDENT_ROLE == loginUser.USER_ROLE)
            {
                this.navBarGroupStudent.Visible = true;
            }
        }

        /* * * *  *  * * * * *
         * GUI Processing
         * * * * * * * * * * */
        private void InitNavbarGroupControls() 
        {
            this.navBarGroupAdmin.Visible = false;
            this.navBarGroupLecturer.Visible = false;
            this.navBarGroupStudent.Visible = false;
        }

        private void barButtonItemLogOff_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitNavbarGroupControls();
            loginUser = null;
            RemoveManagementView();
        }

        private void RemoveManagementView()
        {
            if (m_managementView != null)
            {
                this.splitContainerControl.Panel2.Controls.Remove(m_managementView);
            }
        }
        /* * * *  *  * * * * *
         * Admin Role: User Management View
         * * * * * * * * * * */
        private void navBarItemUserManagement_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            RemoveManagementView();

            m_managementView = new UserManageView();

            m_managementView.TextChanged += new EventHandler(userManagementView_TextChanged);

            m_managementView.Click += new EventHandler(userManagementView_Click);

            m_managementView.Add += new EventHandler(userManagementView_Add);

            m_managementView.Delete += new EventHandler(userManagementView_Delete);

            m_managementView.Update += new EventHandler(userManagementView_Update);

            ((UserManageView)m_managementView).UserIdChange += new EventHandler(userManagementView_UserIdChange);
            
            this.splitContainerControl.Panel2.Controls.Add(m_managementView);

            userManagementView_TextChanged(sender, e);
        }

        private void userManagementView_TextChanged(object sender, EventArgs e)
        {
            IList<SYSUSER> tempList = SysUserBus.FindSysUserListWithName(m_managementView.SearchKey);
            m_managementView.DataSource = tempList;
            m_managementView.DisplayMember = "USER_FIRST_NAME";
            if (tempList.Count > 0)
            {
                m_managementView.SelectedIndex = 0;
                userManagementView_Click(sender, e);
            }
        }

        private void userManagementView_Click(object sender, EventArgs e)
        {
            SYSUSER selectedUser = (SYSUSER)m_managementView.SelectedItem;
            ((UserManageView)m_managementView).UserId = selectedUser.USER_ID;
            ((UserManageView)m_managementView).FirstName = selectedUser.USER_FIRST_NAME;
            ((UserManageView)m_managementView).LastName = selectedUser.USER_LAST_NAME;
            ((UserManageView)m_managementView).Email = selectedUser.USER_EMAIL;
            ((UserManageView)m_managementView).Mobile = selectedUser.USER_MOBILE;
            ((UserManageView)m_managementView).BirthDay = (DateTime)selectedUser.USER_BIRTHDATE;
            ((UserManageView)m_managementView).Address = selectedUser.USER_ADDRESS;
            ((UserManageView)m_managementView).Role = selectedUser.USER_ROLE;
            
        }

        private void userManagementView_Add(object sender, EventArgs e)
        {
            userManagementView_UserIdChange(sender, e);
            string firstName =  ((UserManageView)m_managementView).FirstName;
            string lastName =  ((UserManageView)m_managementView).LastName;
            string userId = ((UserManageView)m_managementView).UserId;

            if (userId == "" || firstName == "" || lastName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                SYSUSER newUser = new SYSUSER();
                newUser.USER_ID = userId;
                newUser.USER_FIRST_NAME = firstName;
                newUser.USER_LAST_NAME = lastName;
                newUser.USER_EMAIL = ((UserManageView)m_managementView).Email;
                newUser.USER_MOBILE = ((UserManageView)m_managementView).Mobile;
                newUser.USER_BIRTHDATE = ((UserManageView)m_managementView).BirthDay;
                newUser.USER_ADDRESS = ((UserManageView)m_managementView).Address;
                newUser.USER_ROLE = ((UserManageView)m_managementView).Role;

                try
                {
                    SysUserBus.AddSysUser(newUser);
                    userManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_ADD, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_ADD, MessageBoxButtons.OK, MessageBoxIcon.Error);
            
                }
            }
           
        }

        private void userManagementView_UserIdChange(object sender, EventArgs e)
        {
            string latestUserId = "0";
            try
            {
                latestUserId  = SysUserBus.GetLatestUser().USER_ID;
            }
            catch
            {
            }
            ((UserManageView)m_managementView).UserId = Utils.GenerateId(latestUserId, Utils.USER_ID_LENGHT);

        }  

        private void userManagementView_Delete(object sender, EventArgs e)
        {
            string userId = ((UserManageView)m_managementView).UserId;

            if (userId == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (userId == loginUser.USER_ID)
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_DELETE_ACTIVE_ACCOUNT, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    SysUserBus.DeleteSysUser(userId);
                    userManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_DELETE, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_DELETE, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void userManagementView_Update(object sender, EventArgs e)
        {
            string firstName = ((UserManageView)m_managementView).FirstName;
            string lastName = ((UserManageView)m_managementView).LastName;
            string userId = ((UserManageView)m_managementView).UserId;

            if (userId == "" || firstName == "" || lastName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SYSUSER modifiedUser = new SYSUSER();
                modifiedUser.USER_ID = userId;
                modifiedUser.USER_FIRST_NAME = firstName;
                modifiedUser.USER_LAST_NAME = lastName;
                modifiedUser.USER_EMAIL = ((UserManageView)m_managementView).Email;
                modifiedUser.USER_MOBILE = ((UserManageView)m_managementView).Mobile;
                modifiedUser.USER_BIRTHDATE = ((UserManageView)m_managementView).BirthDay;
                modifiedUser.USER_ADDRESS = ((UserManageView)m_managementView).Address;
                modifiedUser.USER_ROLE = ((UserManageView)m_managementView).Role;

                try
                {
                    SysUserBus.ModifySysUser(modifiedUser);
                    userManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_MODIFY, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_MODIFY, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        /* * * *  *  * * * * *
         * Admin Role: Course Management View
         * * * * * * * * * * */

        private void navBarItemCourseManagement_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            RemoveManagementView();

            m_managementView = new CourseManageView();

            m_managementView.TextChanged += new EventHandler(courseManagementView_TextChanged);

            m_managementView.Click += new EventHandler(courseManagementView_Click);

            m_managementView.Add += new EventHandler(courseManagementView_Add);

            m_managementView.Delete += new EventHandler(courseManagementView_Delete);

            m_managementView.Update += new EventHandler(courseManagementView_Update);

            ((CourseManageView)m_managementView).CourseIdChange += new EventHandler(courseManagementView_CourseIdChange);

            this.splitContainerControl.Panel2.Controls.Add(m_managementView);

            courseManagementView_TextChanged(sender, e);
        }

        private void courseManagementView_TextChanged(object sender, EventArgs e)
        {  
            IList<COURSE> tempList = CourseBus.FindCourseListWithName(m_managementView.SearchKey);
            m_managementView.DataSource = tempList;
            m_managementView.DisplayMember = "COURSE_NAME";
            if (tempList.Count > 0)
            {
                m_managementView.SelectedIndex = 0;
                courseManagementView_Click(sender, e);
            }
        }

        private void courseManagementView_Click(object sender, EventArgs e)
        {
            COURSE selectedUser = (COURSE)m_managementView.SelectedItem;
            ((CourseManageView)m_managementView).CourseId = selectedUser.COURSE_ID;
            ((CourseManageView)m_managementView).CourseName = selectedUser.COURSE_NAME;
            ((CourseManageView)m_managementView).NumberOfCredit = selectedUser.COURSE_CREDIT;
          
        }

        private void courseManagementView_Add(object sender, EventArgs e)
        {
            courseManagementView_CourseIdChange(sender, e);
            string courseName = ((CourseManageView)m_managementView).CourseName;
            string courseId = ((CourseManageView)m_managementView).CourseId;

            if (courseId == "" || courseName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                COURSE newCourse = new COURSE();
                newCourse.COURSE_ID = courseId;
                newCourse.COURSE_NAME = courseName;
                newCourse.COURSE_CREDIT = ((CourseManageView)m_managementView).NumberOfCredit;

                try
                {
                    CourseBus.AddCourse(newCourse);
                    courseManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_ADD, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_ADD, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
           
        }

        private void courseManagementView_Delete(object sender, EventArgs e)
        {
            string courseId = ((CourseManageView)m_managementView).CourseId;

            if (courseId == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    CourseBus.DeleteCourse(courseId);
                    courseManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_DELETE, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_DELETE, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void courseManagementView_Update(object sender, EventArgs e)
        {
            string courseName = ((CourseManageView)m_managementView).CourseName;
            string courseId = ((CourseManageView)m_managementView).CourseId;


            if (courseId == "" || courseName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                COURSE modifiedCourse = new COURSE();
                modifiedCourse.COURSE_ID = courseId;
                modifiedCourse.COURSE_NAME = courseName;
                modifiedCourse.COURSE_CREDIT = ((CourseManageView)m_managementView).NumberOfCredit;

                try
                {
                    CourseBus.ModifyCourse(modifiedCourse);
                    courseManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_MODIFY, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_MODIFY, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void courseManagementView_CourseIdChange(object sender, EventArgs e)
        {
            string latestCourseId = "0";
            try
            {
                latestCourseId = CourseBus.GetLatestCourse().COURSE_ID;
            }
            catch
            {
            }
            ((CourseManageView)m_managementView).CourseId = Utils.GenerateId(latestCourseId, Utils.COURSE_ID_LENGHT);

        }

       
        /* * * *  *  * * * * *
         * Admin Role: Topic Management View
         * * * * * * * * * * */

        private void navBarItemTopicManagement_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            RemoveManagementView();

            m_managementView = new TopicManageView();

            m_managementView.TextChanged += new EventHandler(topicManagementView_TextChanged);

            m_managementView.Click += new EventHandler(topicManagementView_Click);

            m_managementView.Add += new EventHandler(topicManagementView_Add);

            m_managementView.Delete += new EventHandler(topicManagementView_Delete);

            m_managementView.Update += new EventHandler(topicManagementView_Update);

            ((TopicManageView)m_managementView).TopicIdChange += new EventHandler(topicManagementView_TopicIdChange);

            this.splitContainerControl.Panel2.Controls.Add(m_managementView);

            topicManagementView_TextChanged(sender, e);
        }

        private void topicManagementView_TextChanged(object sender, EventArgs e)
        {
       
            IList<TOPIC> tempList = TopicBus.FindTopicListWithName(m_managementView.SearchKey);
            m_managementView.DataSource = tempList;
            m_managementView.DisplayMember = "TOPIC_NAME";
            if (tempList.Count > 0)
            {
                m_managementView.SelectedIndex = 0;
                topicManagementView_Click(sender, e);
            }
        }

        private void topicManagementView_Click(object sender, EventArgs e)
        {
            TOPIC selectedTopic = (TOPIC)m_managementView.SelectedItem;
            ((TopicManageView)m_managementView).TopicId = selectedTopic.TOPIC_ID;
            ((TopicManageView)m_managementView).TopicName = selectedTopic.TOPIC_NAME;
            ((TopicManageView)m_managementView).TopicDescription = selectedTopic.TOPIC_DESC;

        }

        private void topicManagementView_Add(object sender, EventArgs e)
        {
            topicManagementView_TopicIdChange(sender, e);
            string topicName = ((TopicManageView)m_managementView).TopicName;
            string topicId = ((TopicManageView)m_managementView).TopicId;

            if (topicId == "" || topicName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                TOPIC newTopic = new TOPIC();
                newTopic.TOPIC_ID = topicId;
                newTopic.TOPIC_NAME = topicName;
                newTopic.TOPIC_DESC = ((TopicManageView)m_managementView).TopicDescription;

                try
                {
                    TopicBus.AddTopic(newTopic);
                    topicManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_ADD, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_ADD, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }

        }

        private void topicManagementView_Delete(object sender, EventArgs e)
        {
            string topicId = ((TopicManageView)m_managementView).TopicId;

            if (topicId == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    TopicBus.DeleteTopic(topicId);
                    topicManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_DELETE, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_DELETE, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void topicManagementView_Update(object sender, EventArgs e)
        {
            string topicName = ((TopicManageView)m_managementView).TopicName;
            string topicId = ((TopicManageView)m_managementView).TopicId;


            if (topicId == "" || topicName == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                TOPIC modifiedTopic = new TOPIC();
                modifiedTopic.TOPIC_ID = topicId;
                modifiedTopic.TOPIC_NAME = topicName;
                modifiedTopic.TOPIC_DESC = ((TopicManageView)m_managementView).TopicDescription;


                try
                {
                    TopicBus.ModifyTopic(modifiedTopic);
                    topicManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_MODIFY, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_MODIFY, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void topicManagementView_TopicIdChange(object sender, EventArgs e)
        {
            string latestTopicId = "0";
            try
            {
                latestTopicId  = TopicBus.GetLatestTopic().TOPIC_ID;
            }
            catch
            {
            }
            ((TopicManageView)m_managementView).TopicId = Utils.GenerateId(latestTopicId, Utils.TOPIC_ID_LENGHT);

        }

        /* * * *  *  * * * * *
         * Lecturer Role: Topic Management View
         * * * * * * * * * * */

        private void navBarItemQuestionManagement_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            RemoveManagementView();

            m_managementView = new QuestionManageView();

            m_managementView.TextChanged += new EventHandler(questionManagementView_TextChanged);

            m_managementView.Click += new EventHandler(questionManagementView_Click);

            m_managementView.Add += new EventHandler(questionManagementView_Add);

            m_managementView.Delete += new EventHandler(questionManagementView_Delete);

            m_managementView.Update += new EventHandler(questionManagementView_Update);

            ((QuestionManageView)m_managementView).QuestionIdChange += new EventHandler(questionManagementView_QuestionIdChange);

            this.splitContainerControl.Panel2.Controls.Add(m_managementView);

            questionManagementView_TextChanged(sender, e);
        }

        private void questionManagementView_TextChanged(object sender, EventArgs e)
        {
            IList<QUESTION> tempList = QuestionBus.FindQuestionListWithDescription(m_managementView.SearchKey, loginUser.USER_ID);
            m_managementView.DataSource = tempList;
            m_managementView.DisplayMember = "QUESTION_DESCRIPTION";
            if (tempList.Count > 0)
            {
                m_managementView.SelectedIndex = 0;
                questionManagementView_Click(sender, e);
            }
        }

        private void questionManagementView_Click(object sender, EventArgs e)
        {
            QUESTION selectedQuestion = (QUESTION)m_managementView.SelectedItem;
            ((QuestionManageView)m_managementView).QuestionId = selectedQuestion.QUESTION_ID;
            ((QuestionManageView)m_managementView).QuestionDescription = selectedQuestion.QUESTION_DESCRIPTION;
            ((QuestionManageView)m_managementView).QuestionType = selectedQuestion.QUESTION_TYPE;

            ((QuestionManageView)m_managementView).TopicDataSource = QuestionBus.FindTopicListWithQuestionId(selectedQuestion.QUESTION_ID);
            ((QuestionManageView)m_managementView).TopicDisplayMember = "TOPIC_NAME";

            IList<QUESTION_ITEM_INFO> lstItemInfo = QuestionBus.FindQuestionItemInfoWithQuestionId(selectedQuestion.QUESTION_ID);
            ((QuestionManageView)m_managementView).Option1 = lstItemInfo[0].ITEM_INFO_VALUE;
            ((QuestionManageView)m_managementView).Option2 = lstItemInfo[1].ITEM_INFO_VALUE;
            ((QuestionManageView)m_managementView).Option3 = lstItemInfo[2].ITEM_INFO_VALUE;
            ((QuestionManageView)m_managementView).Option4 = lstItemInfo[3].ITEM_INFO_VALUE;

            IList<QUESTION_SOLUTION> lstSolution = QuestionBus.FindQuestionSolutionListWithQuestionId(selectedQuestion.QUESTION_ID);

            int tempLenght = lstSolution.Count;

            if (tempLenght == 0)
            {
                MessageBox.Show("Chưa có đáp án cho câu hỏi này", FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                List<int> lstIndex = GetSolutionIndex(lstSolution, lstItemInfo);

                foreach (int i in lstIndex)
                {
                    if (i == 0)
                    {
                        ((QuestionManageView)m_managementView).SCheck1 = ((QuestionManageView)m_managementView).MCheck1 = true;
                    }
                    else if (i == 1)
                    {
                        ((QuestionManageView)m_managementView).SCheck2 = ((QuestionManageView)m_managementView).MCheck2 = true;
                    }
                    else if (i ==2)
                    {
                        ((QuestionManageView)m_managementView).SCheck3 = ((QuestionManageView)m_managementView).MCheck3 = true;
                    }
                    else 
                    {
                        ((QuestionManageView)m_managementView).SCheck4 = ((QuestionManageView)m_managementView).MCheck4 = true;
                    }
                }

            }

        }

        private List<int> GetSolutionIndex(IList<QUESTION_SOLUTION> lstSolution, IList<QUESTION_ITEM_INFO> lstItem)
        {
            List<int> lstIndex = new List<int>();
            for(int i = 0;  i < lstSolution.Count; i++)
            {
                for (int j = 0; j < lstItem.Count; j++)
                {
                    if (lstSolution[i].ITEM_ID == lstItem[j].ITEM_ID)
                    {
                        lstIndex.Add(j);
                    }
                }
            }
            return lstIndex;
        }

        private void questionManagementView_Delete(object sender, EventArgs e)
        {
            int questionId = ((QuestionManageView)m_managementView).QuestionId;

            if (questionId.ToString() == "")
            {
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_VALIDATE, FriendlyMessage.MESSAGE_DIALOG_TITLE_WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    IList<QUESTION_ITEM_INFO> lstItemInfo = QuestionBus.FindQuestionItemInfoWithQuestionId(questionId);
                    QuestionBus.DeleteQuestion(questionId, lstItemInfo);
                    questionManagementView_TextChanged(sender, e);
                    MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_DELETE, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_DELETE, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        private void questionManagementView_Add(object sender, EventArgs e)
        {
            try
            {
                QUESTION newQuestion = new QUESTION();
                newQuestion.USER_ID = loginUser.USER_ID;
                newQuestion.QUESTION_DESCRIPTION = ((QuestionManageView)m_managementView).QuestionDescription;
                newQuestion.QUESTION_TYPE = ((QuestionManageView)m_managementView).QuestionType;
                newQuestion.QUESTION_LEVEL = "0";
                QuestionBus.AddQuestion(newQuestion);

                // Add option 1
                bool isSolution1 = (newQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck1 : ((QuestionManageView)m_managementView).MCheck1;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option1, isSolution1, newQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution2 = (newQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck2 : ((QuestionManageView)m_managementView).MCheck2;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option2, isSolution2, newQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution3 = (newQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck3 : ((QuestionManageView)m_managementView).MCheck3;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option3, isSolution3, newQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution4 = (newQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck4 : ((QuestionManageView)m_managementView).MCheck4;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option4, isSolution4, newQuestion.QUESTION_ID);

                // Add Question Topic Map
                ListBox.SelectedObjectCollection lstSelectedTopic = ((QuestionManageView)m_managementView).SelectedTopics;

                foreach (object selectedTopic in lstSelectedTopic)
                {
                    QUESTION_TOPIC_MAP newQTP = new QUESTION_TOPIC_MAP();
                    newQTP.QUESTION_ID = newQuestion.QUESTION_ID;
                    newQTP.TOPIC_ID = ((TOPIC)selectedTopic).TOPIC_ID;

                    QuestionTopicMapBus.AddQuestionTopicMap(newQTP);
                }

                questionManagementView_TextChanged(sender, e);
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_ADD, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_ADD, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void questionManagementView_QuestionIdChange(object sender, EventArgs e)
        {
            ((QuestionManageView)m_managementView).TopicDataSource = TopicBus.GetTopicList();
            ((QuestionManageView)m_managementView).TopicDisplayMember = "TOPIC_NAME";
        }

        private void questionManagementView_Update(object sender, EventArgs e)
        {
            try
            {
                QUESTION selectedQuestion = (QUESTION)m_managementView.SelectedItem;

                selectedQuestion.QUESTION_DESCRIPTION = ((QuestionManageView)m_managementView).QuestionDescription;
                selectedQuestion.QUESTION_TYPE = ((QuestionManageView)m_managementView).QuestionType;

                QuestionBus.ModifyQuestion(selectedQuestion);

                IList<QUESTION_ITEM_INFO> lstItemInfo = QuestionBus.FindQuestionItemInfoWithQuestionId(selectedQuestion.QUESTION_ID);

                // Delete All Question Item Info
                foreach (QUESTION_ITEM_INFO qii in lstItemInfo)
                {
                    QuestionItemInfoDao.DeleteQuestionItemInfo(qii.ITEM_INFO_ID);
                }

                QuestionSolutionDao.DeleteQuestionSolution(selectedQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution1 = (selectedQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck1 : ((QuestionManageView)m_managementView).MCheck1;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option1, isSolution1, selectedQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution2 = (selectedQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck2 : ((QuestionManageView)m_managementView).MCheck2;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option2, isSolution2, selectedQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution3 = (selectedQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck3 : ((QuestionManageView)m_managementView).MCheck3;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option3, isSolution3, selectedQuestion.QUESTION_ID);

                // Add option 1
                bool isSolution4 = (selectedQuestion.QUESTION_TYPE == "SINGLE") ? ((QuestionManageView)m_managementView).SCheck4 : ((QuestionManageView)m_managementView).MCheck4;
                QuestionItemBus.AddQuestionItem(((QuestionManageView)m_managementView).Option4, isSolution4, selectedQuestion.QUESTION_ID);
     
                questionManagementView_TextChanged(sender, e);
                MessageBox.Show(FriendlyMessage.MESSAGE_DIALOG_CONTENT_SUCCESS_MODIFY, FriendlyMessage.MESSAGE_DIALOG_TITLE_SUCCESS, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, FriendlyMessage.MESSAGE_DIALOG_CONTENT_FAIL_MODIFY, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
    }
}